//
//  ProductEditVC.m
//  AFNetworking
//
//  Created by admin on 2020/4/28.
//

#import "ProductEditVC.h"
#import "ProductEditData.h"
#import <objc/message.h>
#include <objc/runtime.h>
#import "UIViewController+Routes.h"
#import "ProductEditBaseCell.h"
#import "ProductDefData.h"
@interface ProductEditVC ()

@property (nonatomic, strong) NSArray<ProductEditData*> * datalist;

@property(strong, nonatomic) UITableView *tableView;

@property(strong, nonatomic) ProductDefData *productData;
@end

@implementation ProductEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
-(NSArray<ProductEditData*> *)datalist{
    if (!_datalist) {
        NSDictionary *dic = [@[
            @{@"title":@"商品类目",
              @"identifie":@"ProductEditRightArrowCell",
              @"height":@44,
              @"pushKey":@"ProductCategoriesSelectVC",
              @"dataKey":@"categotis",
              @"param":@{@"data":self.productData},
              @"sel": @"routepush:param:"
            },
            @{@"title":@"商品名称",
              @"identifie":@"ProductEditRightInput",
              @"placeholder":@"30字以内",
              @"height":@44,
              @"vlaueKey":@"name"
            },
            @{@"title":@"图片设置",
              @"identifie":@"ProductEditImageCell",
              @"height":@(KAutoAcale(114.5)),
              @"vlaueKey":@"name",
              @"pushKey":@"EditProductDefImgVC",
              @"param":@{@"data":self.productData},
              @"sel": @"routepush:param:"
              
            },
            @{@"title":@"商品分类",
              @"identifie":@"ProductEditRightArrowCell",
              @"height":@44,
              @"vlaueKey":@"name",
              @"param":@{@"data":self.productData}
            },
            @{@"title":@"商品价格",
              @"identifie":@"ProductEditRightInput",
              @"placeholder":@"请填写价格",
              @"height":@44,
              @"vlaueKey":@"name"
            },
            @{@"title":@"市场价(选填)",
              @"identifie":@"ProductEditRightInput",
              @"placeholder":@"请填写价格",
              @"height":@44,
              @"vlaueKey":@"name"
            },
            @{@"title":@"商品库存",
              @"identifie":@"ProductEditRightInput",
              @"placeholder":@"请填写库存",
              @"height":@44,
              @"vlaueKey":@"name"
            },
            @{@"title":@"商品详情",
              @"identifie":@"ProductEditRightArrowCell",
              @"height":@44,
              @"vlaueKey":@"name",
              @"pushKey":@"ProductDefVC",
              @"param":@{@"data":self.productData},
              @"sel": @"routepush:param:"
              
            }
        ] mutableCopy];
        _datalist = [NSArray modelArrayWithClass:[ProductEditData class] json:dic];
    }
    return _datalist;
}
-(ProductDefData *)productData{
    if (!_productData) {
        _productData = [ProductDefData new];
    }
    return _productData;
}

-(void)configUI{
    self.title = @"发布本店商品";
    [self.view addSubview:self.tableView];
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        
        
        for (ProductEditData *data in self.datalist) {
            
            Class cls = NSClassFromString(data.identifie);
            [_tableView registerClass:cls forCellReuseIdentifier:data.identifie];
        }
        
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datalist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ProductEditData *data = [self.datalist objectAtIndex:indexPath.row];
    ProductEditBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:data.identifie forIndexPath:indexPath];
    cell.productData = _productData;
    SEL sel = NSSelectorFromString(@"reload:");
    if ([cell respondsToSelector:sel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [cell performSelector:sel withObject:data];
#pragma clang diagnostic pop
    }
    BOOL ishidden = (indexPath.row == (_datalist.count-1));
    [cell.lineView setHidden:ishidden];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductEditData *data = [self.datalist objectAtIndex:indexPath.row];
    return data.height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductEditData *data = [self.datalist objectAtIndex:indexPath.row];
    if (data.sel) {
        if ([data.sel isEqualToString:@"routepush:param:"]) {
            [self routepush:data.pushKey param:data.param];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    headview.backgroundColor = UIColor.whiteColor;
    [headview ug_radius:10 addcorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    return headview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    footview.backgroundColor = UIColor.whiteColor;
    [footview ug_radius:10 addcorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
    return footview;
}



-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(KPAND_DEF);
        make.right.mas_equalTo(self.view).mas_offset(-KPAND_DEF);
        make.top.mas_equalTo(self.view).mas_offset(KPAND_DEF);
        make.bottom.mas_equalTo(self.view).mas_offset(-KPAND_DEF);
    }];
}



@end
